﻿using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace CodeBlocks
{
    // This is simply a utility that extracts the current version number from the nuspec file
    // It is NOT a code block
    static class Program
    {
        private static readonly Regex VersionRegex = new Regex(@"<version>(?<version>\d\.\d\.\d\.\d)</version>", RegexOptions.Compiled);
        
        private static void PrintUsage(int returnValue = -1) 
        { 
            Console.WriteLine("Usage is CodeBlocks <nuspec filename>");
            Environment.Exit(returnValue);
        }

        static int Main(string[] args)
        {
            var filename = string.Empty;
            args.Process(() => PrintUsage(),
                         new CommandLine.Switch("nuspecfilename", s => filename = s.First()));
            if (!File.Exists(filename))
                PrintUsage();

            string nuspecText;
            using (var reader = new StreamReader(filename))
                nuspecText = reader.ReadToEnd();
            var match = VersionRegex.Match(nuspecText);
            if (!match.Success)
                return 1;

            Console.WriteLine(match.Groups["version"].ToString());
            return 0;
        }
    }
}